<template>
  <page class="product-execu-qisbase">
    <template slot="title">Qis测试</template>
    <div class="table-page-search-wrapper">
      <p-form layout="inline">
        <p-row :gutter="24">
          <p-col :xl="8">
            <p-form-item label="工序工单">
              <p-input :value="data.processBillNo" disabled />
            </p-form-item>
          </p-col>
          <p-col :xl="8">
            <p-form-item label="产品编码">
              <p-input :value="data.productCode" disabled />
            </p-form-item>
          </p-col>
          <p-col :xl="8">
            <p-form-item label="产品描述">
              <p-input :value="data.productName" disabled />
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </div>
    <p-table
      :loading="loading"
      :columns="columns"
      :data-source="dataSource"
      :pagination="false"
      :scroll="{ x: '100%' }"
      row-key="id"
      size="small"
      bordered
    >
      <!-- 天线类型，输入测试记录 -->
      <template slot="testValue" slot-scope="text, record">
        <template v-if="finished || record.testMethod === '自动'">{{
          text
        }}</template>
        <template v-else>
          <!--1-计量，2-计数，计量类型的手动输入数值，计数类型的选择pass/ng-->
          <p-input-number
            v-if="record.dataType === 1"
            v-model="record.testValue"
          />
          <p-select
            v-else-if="record.dataType === 2"
            v-model="record.testValue"
            style="width: 88px"
          >
            <p-select-option :value="1">PASS</p-select-option>
            <p-select-option :value="2">NG</p-select-option>
          </p-select>
        </template>
      </template>
      <!-- 设备类型，输入测试记录下限 -->
      <template slot="testValueLow" slot-scope="text, record">
        <template v-if="finished || record.testMethod === '自动'">{{
          text
        }}</template>
        <template v-else>
          <!--1-计量，2-计数，计量类型的手动输入数值，计数类型的选择pass/ng-->
          <p-input-number
            v-if="record.dataType === 1"
            v-model="record.testValueLow"
          />
          <p-select
            v-else-if="record.dataType === 2"
            v-model="record.testValueLow"
            style="width: 88px"
          >
            <p-select-option :value="1">PASS</p-select-option>
            <p-select-option :value="2">NG</p-select-option>
          </p-select>
        </template>
      </template>
      <!-- 设备类型，输入测试记录上限 -->
      <template slot="testValueUp" slot-scope="text, record">
        <template v-if="finished || record.testMethod === '自动'">{{
          text
        }}</template>
        <template v-else>
          <!--1-计量，2-计数，计量类型的手动输入数值，计数类型的选择pass/ng-->
          <p-input-number
            v-if="record.dataType === 1"
            v-model="record.testValueUp"
          />
          <p-select
            v-else-if="record.dataType === 2"
            v-model="record.testValueUp"
            style="width: 88px"
          >
            <p-select-option :value="1">PASS</p-select-option>
            <p-select-option :value="2">NG</p-select-option>
          </p-select>
        </template>
      </template>
    </p-table>
    <div class="text-right mt20">
      <p-button :disabled="disabled" type="primary" @click="save(0)"
        >保存</p-button
      >
      <p-button
        :disabled="disabled"
        class="ml10"
        type="primary"
        @click="save(1)"
        >提交</p-button
      >
    </div>
  </page>
</template>

<script>
import mixins from "./info-mixins";
import { getAction, postAction } from "@/api/manage";
import { isEmpty } from "@/utils/util";

export default {
  mixins: [mixins],
  props: {
    data: {
      type: Object,
    },
  },
  data() {
    return {
      // 分类，2为天线，1为设备
      type: "1",
      finished: 0,
    };
  },
  computed: {
    disabled() {
      return !this.dataSource || !this.dataSource.length;
    },
    columns() {
      const header =
        this.type === "1" ? this.getDeviceHeader() : this.getAerialHeader();
      const { requirement, input } = header;
      const columns = [
        {
          title: "序号",
          dataIndex: "rowIndex",
          align: "center",
          fixed: "left",
          width: 50,
          customRender(text, record, index) {
            return index + 1;
          },
        },
        {
          title: "测试项目",
          dataIndex: "itemName",
          align: "center",
          width: 160,
          fixed: "left",
        },
        {
          title: "测试项目说明",
          dataIndex: "itemDescription",
          align: "center",
          width: 200,
        },
        {
          title: "指标要求",
          children: requirement,
        },
        {
          title: "测试记录",
          fixed: "right",
          children: [
            {
              title: "测试方式",
              dataIndex: "testMethod",
              align: "center",
              width: 80,
            },
            ...input,
          ],
        },
      ];

      return columns;
    },
  },
  mounted() {
    this.getTemplate();
  },
  methods: {
    getDeviceHeader() {
      return {
        requirement: [
          {
            title: "指标要求(下行)",
            dataIndex: "indexLow",
            align: "center",
            width: 100,
          },
          {
            title: "指标要求(上行)",
            dataIndex: "indexUp",
            align: "center",
            width: 100,
          },
          {
            title: "数据属性",
            dataIndex: "dataType",
            align: "center",
            width: 80,
            customRender: (text) => this.getDataType(text),
          },
          {
            title: "下行下限",
            dataIndex: "indexLowLow",
            align: "center",
            width: 100,
          },
          {
            title: "下行上限",
            dataIndex: "indexLowUp",
            align: "center",
            width: 100,
          },
          {
            title: "上行下限",
            dataIndex: "indexUpLow",
            align: "center",
            width: 100,
          },
          {
            title: "上行上限",
            dataIndex: "indexUpUp",
            align: "center",
            width: 100,
          },
          {
            title: "初调岗位下行",
            dataIndex: "initTestLow",
            align: "center",
            width: 100,
          },
          {
            title: "初调岗位上行",
            dataIndex: "initTestUp",
            align: "center",
            width: 100,
          },
          {
            title: "复调岗位下行",
            dataIndex: "repeatTestLow",
            align: "center",
            width: 100,
          },
          {
            title: "复调岗位上行",
            dataIndex: "repeatTestUp",
            align: "center",
            width: 100,
          },
          {
            title: "默认值",
            dataIndex: "defaultValue",
            align: "center",
            width: 100,
          },
        ],
        input: [
          {
            title: "测试记录下行",
            dataIndex: "testValueLow",
            align: "center",
            width: 100,
            scopedSlots: {
              customRender: "testValueLow",
            },
          },
          {
            title: "测试记录上行",
            dataIndex: "testValueUp",
            align: "center",
            width: 100,
            scopedSlots: {
              customRender: "testValueUp",
            },
          },
        ],
      };
    },
    getAerialHeader() {
      return {
        requirement: [
          {
            title: "指标要求",
            dataIndex: "index",
            align: "center",
            width: 100,
          },
          {
            title: "数据属性",
            dataIndex: "dataType",
            align: "center",
            width: 80,
            customRender: (text) => this.getDataType(text),
          },
          {
            title: "测试指标下限",
            dataIndex: "indexLow",
            align: "center",
            width: 100,
          },
          {
            title: "测试指标上限",
            dataIndex: "indexLowUp",
            align: "center",
            width: 100,
          },
          {
            title: "默认值",
            dataIndex: "defaultValue",
            align: "center",
            width: 100,
          },
          {
            title: "初调岗位",
            dataIndex: "initTest",
            align: "center",
            width: 100,
          },
          {
            title: "复调岗位",
            dataIndex: "repeatTest",
            align: "center",
            width: 100,
          },
        ],
        input: [
          {
            title: "测试记录",
            dataIndex: "testValue",
            align: "center",
            width: 100,
            scopedSlots: {
              customRender: "testValue",
            },
          },
        ],
      };
    },
    getDataType(text) {
      let type;
      switch (text) {
        case 1:
          type = "计量";
          break;
        case 2:
          type = "计数";
          break;
      }

      return type;
    },
    getTemplate() {
      if (!this.data || !this.data.productCode) {
        return;
      }
      const { ifFirst, productCode, barCode } = this.data;
      this.loading = true;
      if (ifFirst === 1) {
        getAction('/mes-manufacture/mfQisTest/getQisTestResult', {
          barCode,
        }).then((res) => {
          let data = [];
          if (res.data) {
            const { templateType, childs, finishFlag } = res.data;
            this.setFinished(finishFlag);
            this.type = templateType;
            data = childs;
          }
          this.dataSource = data;
          this.loading = false;
        });
      } else {
        const mcode = productCode;
        getAction('/basic-business/mesBaQistemplate/getTemplateByMcode', {
          mcode,
        }).then((res) => {
          let data = [];
          if (res.data) {
            this.type = res.data.classification;
            if (res.data.classification === "1") {
              res.data.equipmentListDtos.forEach((item) => {
                data.push({
                  itemName: item.objectName,
                  itemDescription: item.objectSrc,
                  dataType: item.dataAttribute === "计量" ? 1 : 2,
                  defaultValue: item.defaultValue,
                  testMethod: item.method,
                  indexLow: item.requirementLower,
                  indexUp: item.requirementUpper,
                  indexLowLow: item.downLower,
                  indexLowUp: item.downUpper,
                  indexUpLow: item.upstreamLower,
                  indexUpUp: item.upstreamUpper,
                  initTestLow: item.initialStationLower,
                  initTestUp: item.initialStationUpper,
                  repeatTestLow: item.repeatStationLower,
                  repeatTestUp: item.repeatStationUpper,
                  testValueLow: "",
                  testValueUp: "",
                });
              });
            } else {
              res.data.antennaListDtos.forEach((item) => {
                data.push({
                  itemName: item.objectName,
                  itemDescription: item.objectSrc,
                  dataType: item.dataAttribute === "计量" ? 1 : 2,
                  defaultValue: item.defaultValue,
                  testMethod: item.method,
                  initTest: item.initialStation,
                  repeatTest: item.repeatStation,
                  index: item.requirement,
                  indexLow: item.targetLower,
                  indexUp: item.targetUpper,
                  testValue: "",
                });
              });
            }
          }
          this.setFinished("");
          this.dataSource = data;
          this.loading = false;
        });
      }
    },
    setFinished(finished) {
      this.finished = finished;
      this.$emit("qis-status", finished);
    },
    save(finishFlag = 0) {
      if (this.data) {
        this.$message.error("缺少工序工单id");
        return;
      }
      if (finishFlag) {
        const item = this.dataSource.find((item) => {
          let empty;
          if (this.type === "1") {
            empty = isEmpty(item.testValueLow) || isEmpty(item.testValueUp);
          } else {
            empty = isEmpty(item.testValue);
          }
          return item.testMethod !== "自动" && empty;
        });
        if (item) {
          this.$message.error("请先完善测试项目内容");
          return;
        }
      }
      const data = {
        finishFlag,
        pnsn: this.data.barCode,
        processBillId: this.data.processBillId,
        templateType: this.type,
        childs: this.dataSource,
      };
      postAction("/mes-manufacture/mfQisTest/saveQisTestResult", data).then(
        (res) => {
          if (res.data) {
            this.$message.success(finishFlag ? "提交成功" : "保存成功");
            this.setFinished(finishFlag);
          }
        }
      );
    },
  },
};
</script>

<style lang="less">
.product-execu-qisbase {
  height: 100%;
  overflow: auto;
}
</style>